var dbConfig = require("../utils/dbconfig");
var express = require("express");
var Response = require("../utils/response");

async function getPosition(req, res, next) {
  let {
    id,
    realname,
    address,
    log_time_punch,
    vacation_no,
    log_time_checkout,
    username,
    staff_id,
    date,
  } = req.body;
  // let staff_id = 1;
  // console.log(
  //   id,
  //   realname,
  //   ip,
  //   address,
  //   log_time_punch,
  //   vacation_no,
  //   log_time_checkout,
  //   username,
  //   staff_id
  // );
  // let punchInfo = await judgePunch(
  //   staff_id,
  //   log_time_punch,
  //   log_time_checkout,
  //   username
  // );
  // let t  = JSON.stringify(JSON.parse(punchInfo))
  let vacation = await searchVacation(staff_id);
  // let userinfo = await getUserPunchInfo(username);
  vacation_no = vacation[0].vacation_no;
  date = "1";
  var sql =
    "INSERT INTO base_logs (id,realname,address,`log_time_punch`,`vacation_no`,`log_time_checkout`,`date`) VALUES (?,?,?,?,?,?,?)";
  var sqlArr = [
    id,
    realname,
    address,
    log_time_punch,
    vacation_no,
    log_time_checkout,
    date
  ];
  // console.log(punchInfo[0].realname, userinfo,vacation);
  var callBack = (err, data) => {
    if (err) {
      console.log("连接出错了", err);
    } else {
      if (data.affectedRows == 1) {
        res.send(new Response(200, "打卡成功", data));
        // res.send(data, punchInfo, userinfo);
      } else {
        res.send(new Response(500, "打卡失败", data));
      }
    }
  };
  dbConfig.sqlConnection(sql, sqlArr, callBack);
}

async function judgePunch(
  staff_id,
  log_time_punch,
  log_time_checkout,
  username
) {
  var sql =
    "select * from base_logs where `staff_id`=? and `log_time_punch` like '%' or `staff_id`=? and `log_time_checkout` like '%'";
  var sqlArr = [staff_id, log_time_punch, log_time_checkout];
  // let userinfo  = await getUserPunchInfo(username)
  return dbConfig.sql_connect_async(sql, sqlArr);
  // var callBack = (err, data) => {
  //   if (err) {
  //     console.log("连接出错了");
  //   } else {
  //     if (data.length > 0) {
  //       res.send(new Response(200, "查询成功", data));
  //     } else {
  //       res.send(new Response(500, "查询失败", data));
  //     }
  //   }
  // };
  // dbConfig.sqlConnection(sql, sqlArr, callBack);
}

async function getUserPunchInfo(username) {
  var sql = "select * from base_user where username = ?";
  var sqlArr = [username];
  return dbConfig.sql_connect_async(sql, sqlArr);
  // var callBack = (err, data) => {
  //   if (err) {
  //     console.log("连接出错了");
  //   } else {
  //     if (data.length > 0) {
  //       res.send(new Response(200, "查询成功", data));
  //       return data
  //     } else {
  //       res.send(new Response(500, "查询失败", data));
  //     }
  //   }
  // };
  // dbConfig.sqlConnection(sql, sqlArr, callBack);
}

async function searchVacation(staff_id) {
  var sql = "select * from staff_vacation where staff_id=?";
  var sqlArr = [staff_id];
  return dbConfig.sql_connect_async(sql, sqlArr);
}

module.exports = { getPosition };
